package com.zlzlib.liblayoutmanager;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Desc: 自动滚动的列表
 * @Copyright:
 * @DateTime: 2021/8/18 11:21
 * @Author zlz
 * @Version 1.0
 */
public class AutoScrollRecyclerView extends RecyclerView {

    private final AutoScrollSnapUtils autoScrollSnapUtils;

    public AutoScrollRecyclerView(@NonNull Context context) {
        this(context, null);
    }

    public AutoScrollRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoScrollRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AutoScrollRecyclerView);
        final int scrollType = typedArray.getInt(R.styleable.AutoScrollRecyclerView_scrollType, AutoScrollSnapUtils.NORMAL);
        final int timeInterval = typedArray.getInt(R.styleable.AutoScrollRecyclerView_timeInterval,
                scrollType == AutoScrollSnapUtils.NORMAL ? AutoScrollSnapUtils.TIME_INTERVAL / 10 : AutoScrollSnapUtils.TIME_INTERVAL);
        final int direction = typedArray.getInt(R.styleable.AutoScrollRecyclerView_direction, AutoScrollSnapUtils.FORWARD);
        final int moveSpeed = typedArray.getInt(R.styleable.AutoScrollRecyclerView_moveSpeed, AutoScrollSnapUtils.MOVE_STEP);
        typedArray.recycle();
        autoScrollSnapUtils = new AutoScrollSnapUtils(timeInterval, direction, scrollType, moveSpeed);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        boolean result = super.dispatchTouchEvent(ev);
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (autoScrollSnapUtils != null) {
                    autoScrollSnapUtils.pause();
                }
                break;
            case MotionEvent.ACTION_UP:
                if (autoScrollSnapUtils != null) {
                    autoScrollSnapUtils.start();
                }
        }
        return result;
    }

    public void start() {
        autoScrollSnapUtils.start();
    }

    public void pause() {
        autoScrollSnapUtils.pause();
    }

    public void setTimeInterval(int timeInterval) {
        autoScrollSnapUtils.setTimeInterval(timeInterval);
    }

    public void setDirection(int direction) {
        autoScrollSnapUtils.setDirection(direction);
    }

    public void setMoveStep(int moveSpeed) {
        autoScrollSnapUtils.setMoveStep(moveSpeed);
    }

    public void destroy(){
        autoScrollSnapUtils.destroyCallbacks();
    }

    @Override
    public void setLayoutManager(LayoutManager layout) {
        super.setLayoutManager(layout);
        autoScrollSnapUtils.attachToRecyclerView(this);
    }
}
